local blaze = require "blaze"
local app = require "app"
local gamelib = require "club.threecard.gamelib"
local pokerlib = require "club.threecard.pokerlib"
local poker_manager = pokerlib.core.poker_manager
local Vector3 = CS.UnityEngine.Vector3
local zero = Vector3.zero

local BlazeNS = CS.Blaze
local GameObjectExtension = BlazeNS.GameObjectExtension

local poker = gamelib.ui.element()

local log = blaze.logging.get("poker")

local default_res = "club.threecard.pokerlib/ui/common.b:poker"

function poker:ctor(res)
    self.res = res or default_res
end

function poker:on_injected(bind)
    bind("trigger.click", self.trigger_listener, self._click)
    bind("trigger.down", self.trigger_listener, self._down)
    bind("trigger.enter", self.trigger_listener, self._enter)
    bind("trigger.exit", self.trigger_listener, self._exit)
    bind("trigger.up", self.trigger_listener, self._up)
end

function poker:_click()
    if not self.click then
        return
    end
    self:click()
end

function poker:_down()
    if not self.down then
        return
    end
    self:down()
end

function poker:_enter()
    if not self.enter then
        return
    end
    self:enter()
end

function poker:_exit()
    if not self.exit then
        return
    end
    self:exit()
end

function poker:_up()
    if not self.up then
        return
    end
    self:up()
end

function poker:on_get()
end

function poker:on_put()
    self:set_position(zero)
end

function poker:on_click()
end

function poker:will_select(b)
    if self._will_select == b then
        return
    end
    self._will_select = b
    self:lazy("on_will_select")
end

function poker:on_will_select()
    self.select_flag.gameObject:SetActive(self._will_select)
end

function poker:select()
    self.selected = not self.selected
    if self.selected then
        self:lazy("on_select")
    else
        self:lazy("on_unselect")
    end
    self:will_select(false)
end

function poker:on_select()
    local pos = Vector3(self.position.x, self.position.y + 30, self.position.z)
    self:set_position(pos)
end

function poker:on_unselect()
    local pos = Vector3(self.position.x, 0, self.position.z)
    self:set_position(pos)
end

function poker:clone()
    return poker_manager.get(self.data, self.poker_class)
end

function poker:unselect()
    self.selected = false
    self:will_select(false)
    self:lazy("on_unselect")
end

function poker:tostring()
    return self.data:tostring()
end

-- 一定是同步加载
function poker:on_load()
    if self.res then
        local ok, result = self.app.res.load(self.res, {sync = true})
        if ok then
            local gameobject = GameObjectExtension.GetFromPool(result)
            self.pooled = true
            self:set_gameobject(gameobject, true)
        else
            error(result)
        end
    end

    if self.store_watchers then
        for name, handlers in pairs(self.store_watchers) do
            local store = self.app.store[name]
            if not store then
                log.warn(string.format("store '%s' not found", name))
            end
            local state = store.state
            for event_name, handler in pairs(handlers) do
                store.watch(self, event_name)
                handler(self, store, state, blaze.empty, true)
            end
        end
    end
end

function poker:set_data(data)
    if self.data == data then
        return
    end

    self.data = data
    self:lazy("on_set_data")
end

function poker:on_set_data()
    self:start_coroutine(function()
        local client_type = self.data.client_type

        local value = nil
        local type_small = nil
        local type_big = nil

        if client_type == pokerlib.core.poker_type.null then
            local uri = "club.threecard.pokerlib/atlases/pokercard.b:paibei:UnityEngine.Sprite, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
            local isOk, result = app.res.load(uri)
            if isOk then
                self.bg.sprite = result
            end
            self.value.gameObject:SetActive(false)
            self.type_small.gameObject:SetActive(false)
            self.type_big.gameObject:SetActive(false)
            return
        end
            
        self.value.gameObject:SetActive(true)
        self.type_small.gameObject:SetActive(true)
        self.type_big.gameObject:SetActive(true)
        if client_type == pokerlib.core.poker_type.hei then
            value = "num_black_"..self.data.client_value
            type_small = "heitao_small"
            type_big = "heitao_big"
            self.value.transform.localPosition = Vector3(-47, 70, 0)
        elseif client_type == pokerlib.core.poker_type.hua then
            value = "num_black_"..self.data.client_value
            type_small = "caohua_small"
            type_big = "caohua_big"
            self.value.transform.localPosition = Vector3(-47, 70, 0)
        elseif client_type == pokerlib.core.poker_type.hong then
            value = "num_red_"..self.data.client_value
            type_small = "hongtao_small"
            type_big = "hongtao_big"
            self.value.transform.localPosition = Vector3(-47, 70, 0)
        elseif client_type == pokerlib.core.poker_type.pian then
            value = "num_red_"..self.data.client_value
            type_small = "fangkuai_small"
            type_big = "fangkuai_big"
            self.value.transform.localPosition = Vector3(-47, 70, 0)
        elseif client_type == pokerlib.core.poker_type.maoer then
            if self.data.client_value == 14 then
                value = "black_joker"
            elseif self.data.client_value == 15 then
                value = "red_joker"
            end
            type_small = nil
            type_big = nil
            self.value.transform.localPosition = Vector3(-47, 0, 0)
        end

        local uri = "club.threecard.pokerlib/atlases/pokercard.b:dipai:UnityEngine.Sprite, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        local isOk, result = app.res.load(uri)
        if isOk then
            self.bg.sprite = result
        end

        local uri = "club.threecard.pokerlib/atlases/pokercard.b:" .. value .. ":UnityEngine.Sprite, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        local isOk, result = app.res.load(uri)
        if isOk then
            self.value.sprite = result
        end

        self.type_small.gameObject:SetActive(type_small~=nil)
        if type_small then
            local uri = "club.threecard.pokerlib/atlases/pokercard.b:" .. type_small .. ":UnityEngine.Sprite, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
            local isOk, result = app.res.load(uri)
            if isOk then
                self.type_small.sprite = result
            end
        end

        self.type_big.gameObject:SetActive(type_big~=nil)
        if type_big then
            local uri = "club.threecard.pokerlib/atlases/pokercard.b:" .. type_big .. ":UnityEngine.Sprite, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
            local isOk, result = app.res.load(uri)
            if isOk then
                self.type_big.sprite = result
            end
        end

        self.value:SetNativeSize()
        self.type_small:SetNativeSize()
        self.type_big:SetNativeSize()
     end)
end

return poker